﻿using Ookii.Dialogs.Wpf;

using PLCS.App.Shared.Extensions;
using PLCS.App.Shared.Models;
using PLCS.App.Shared.Service;
using PLCS.Application.Contracts.EquipmentApp.Dtos;
using PLCS.Application.Contracts.LogShowApp.Dtos;
using PLCS.Application.Contracts.StationApp.Dtos;
using PLCS.Client.IService.LogManager;
using PLCS.Domain.Equipments;
using PLCS.Domain.Shared.Enums;
using PLCS.Domain.Shared.Stations;
using PLCS.Shared.Common.Constant;

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PLCS.App.EquipmentManager.ViewModels.Equipment;

internal class EquipmentViewModel : NavigationPagedCurdViewModel<EquipmentDto, EquipmentEditDto, IEquipmentService>
{
    private readonly IOperationLogService _logShowService;
    private readonly CacheSettingService _cacheSettingService;
    private readonly IStationService _stationService;

    public DelegateCommand ImportCommand { get; set; }

    private string _equipmentNo, _equipmentType;

    public string EquipmentNo
    {
        get { return _equipmentNo; }
        set { SetProperty(ref _equipmentNo, value); }
    }

    public string EquipmentType
    {
        get { return _equipmentType; }
        set { SetProperty(ref _equipmentType, value); }
    }

    public EquipmentViewModel(IEquipmentService service, IOperationLogService logShowService, CacheSettingService cacheSettingService, IStationService stationService) : base(service)
    {
        ImportCommand = new DelegateCommand(Import);
        _logShowService = logShowService;
        _cacheSettingService = cacheSettingService;
        _stationService = stationService;
        View = "设备管理";
    }

    public override async Task GetPageData(int pageIndex)
    {
        try
        {
            UpdateLoading(true);
            var res = await _service.GetListBySearchAsync((pageIndex - 1) * PageSize, PageSize, EquipmentNo, EquipmentType);
            TotalCount = (int)res.TotalCount;
            SetPageCount();
            DataGridList = res.Items.ToObservable();
        }
        finally
        {
            UpdateLoading(false);
        }
    }

    private string KeepOnlyDigits(string input)
    {
        string pattern = "[^0-9]";
        return Regex.Replace(input, pattern, "").Trim();
    }

    private async void Import()
    {
        try
        {
            var fileDialog = new VistaOpenFileDialog();
            fileDialog.ShowDialog();

            var fileName = fileDialog.FileName;

            if (fileName == null)
                return;

            // 读取excel导入到DataTable
            var table = TableHelper.ExcelToTable(fileName);
            if (table == null) return;
            // 将DataTable转为list
            var equipmentDtos = table.ToList<EquipmentEditDto>();
            // 开始导入
            await _service.CreateEquipmentListAsync(equipmentDtos);
            Log("导入", equipmentDtos);
            ShowMessage("导入成功");
            // 导入完刷新
            await RefreshAsync();
        }
        catch (System.Exception ex)
        {
            ShowMessage(ex.Message);
        }
    }
}